#include "usart.h"
#include "app.h"
#include "FreeRTOS.h"					 
#include "task.h" 
#include "event_groups.h"

UART_HandleTypeDef huart1;
UART_HandleTypeDef huart2;


int fputc(int ch, FILE *fp)
{
	HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, 0xFFFF);
	return ch;
}


void MX_USART1_UART_Init(void)
{
  huart1.Instance = USART1;
  huart1.Init.BaudRate = 115200;
  huart1.Init.WordLength = UART_WORDLENGTH_8B;
  huart1.Init.StopBits = UART_STOPBITS_1;
  huart1.Init.Parity = UART_PARITY_NONE;
  huart1.Init.Mode = UART_MODE_TX_RX;
  huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  if (HAL_UART_Init(&huart1) != HAL_OK)
  {
    Error_Handler();
  }
}

void MX_USART2_UART_Init(void)
{
  huart2.Instance = USART2;
  huart2.Init.BaudRate = 9600;
  huart2.Init.WordLength = UART_WORDLENGTH_8B;
  huart2.Init.StopBits = UART_STOPBITS_1;
  huart2.Init.Parity = UART_PARITY_NONE;
  huart2.Init.Mode = UART_MODE_TX_RX;
  huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  huart2.Init.OverSampling = UART_OVERSAMPLING_16;
  if (HAL_UART_Init(&huart2) != HAL_OK)
  {
    Error_Handler();
  }
}


void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle)
{

  GPIO_InitTypeDef GPIO_InitStruct = {0};
  if(uartHandle->Instance == USART1)
  {
  /* USER CODE BEGIN USART1_MspInit 0 */

  /* USER CODE END USART1_MspInit 0 */
    /* USART1 clock enable */
    __HAL_RCC_USART1_CLK_ENABLE();

    __HAL_RCC_GPIOA_CLK_ENABLE();
    /**USART1 GPIO Configuration
    PA9     ------> USART1_TX
    PA10     ------> USART1_RX
    */
    GPIO_InitStruct.Pin = GPIO_PIN_9|GPIO_PIN_10;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF7_USART1;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

  /* USER CODE BEGIN USART1_MspInit 1 */

  /* USER CODE END USART1_MspInit 1 */
  }
  else  if(uartHandle->Instance == USART2)
  {
    __HAL_RCC_USART2_CLK_ENABLE();
    __HAL_RCC_GPIOA_CLK_ENABLE();
    GPIO_InitStruct.Pin = GPIO_PIN_2|GPIO_PIN_3;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF7_USART2;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

    HAL_NVIC_SetPriority(USART2_IRQn, 0, 0);
    HAL_NVIC_EnableIRQ(USART2_IRQn);
  }
}

void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle)
{

  if(uartHandle->Instance==USART1)
  {
  /* USER CODE BEGIN USART1_MspDeInit 0 */

  /* USER CODE END USART1_MspDeInit 0 */
    /* Peripheral clock disable */
    __HAL_RCC_USART1_CLK_DISABLE();

    /**USART1 GPIO Configuration
    PA9     ------> USART1_TX
    PA10     ------> USART1_RX
    */
    HAL_GPIO_DeInit(GPIOA, GPIO_PIN_9|GPIO_PIN_10);

  }
}

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)               	//串口1中断服务程序
{
    static int count = 0;
    BaseType_t xHigherPriorityTaskWoken;
    uint32_t ulReturn;
    
    if(huart == &huart2)
    {
        if(rev == '$')
        {
            count = 0;
        }

        g_data_buffer[count++] = rev;

        // For "GPRMC/GNRMC"
        if(g_data_buffer[0] == '$' && g_data_buffer[4] == 'M' && g_data_buffer[5] == 'C')
        {
            if(rev == '\n')									   
            {
                memset(GPSData.GPS_Buffer, 0, GPS_Buffer_Length);      //清空
                memcpy(GPSData.GPS_Buffer, g_data_buffer, count); 	//保存数据
                GPSData.isGetData = TRUE;
                count = 0;
                memset(g_data_buffer, 0, REC_BUFF_LEN);      //清空

                
                
                /* 进入临界段，临界段可以嵌套 */
                ulReturn = taskENTER_CRITICAL_FROM_ISR();
                
                xEventGroupSetBitsFromISR(uartDataCompleteHandle, RTOS_UART_EVENT, &xHigherPriorityTaskWoken);
                
                // 如果需要的话进行一次任务切换
                portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
                /* 退出临界段 */
                taskEXIT_CRITICAL_FROM_ISR(ulReturn);
            }	
                    
        }
        
        if(count >= REC_BUFF_LEN)
        {
            count = REC_BUFF_LEN;
        }
        HAL_UART_Receive_IT(&huart2, (uint8_t *)&rev, 1);
    }
    
	
}

